package com.example.demo.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.entity.Car;
import com.example.demo.service.CarService;

@Controller
@RequestMapping("car")
public class CarController {
	
	@Autowired
	private CarService carService;

	@RequestMapping("add")
	@ResponseBody
	public Object addCar(Car car) {
		Car add = carService.add(car);
		return add;
	}
	@RequestMapping("update_view")
	public String updateView(Integer id,Map<String,Object> map) {
		Car findOne = carService.findOne(id);
		map.put("car", findOne);
		return "car/insert";
	}
	@RequestMapping("update")
	public String update(Car car) {
		Car update = carService.update(car);
		return "redirect:all";
	}
	
	@RequestMapping("all")
	public String all(Map<String,Object> map) {
		List<Car> cars = carService.findAll();
		map.put("cars", cars);
		return "car/all";
	}
	
}
